<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Permission;
use App\Http\Model\Role;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class RoleController extends Controller
{
//    给角色添加相应权限
    public function auth()
    {
//       1 接收传过来的角色、要赋予的权限数据
         $input = Input::all();
//         dd($input);
//         角色ID
        $role_id = $input['role_id'];
        $pers_id = $input['permission_id'];

//        删除当期角色已经有的权限
        $re =  DB::table('permission_role')->where('role_id',$role_id )->delete();
        if(!$re){
            back()->with('errors','授权失败，请重新授权');
        }

//       2 授权（向permission_role表中写记录）
        foreach ($pers_id as $v) {
            \DB::table('permission_role')->insert(
                ['role_id' => $role_id, 'permission_id' => $v]
            );
        }
        return redirect('admin/role');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $data =  Role::get();
       return view('admin.role.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
       $input =  Input::except('_token');
       $role = Role::create($input);
       if($role){
           return redirect('admin/role');
       }else{
           return back()->with('errors','添加失败');
       }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //1 获取到当前角色
        $role =   Role::find($id);
//        2 获取到所有的权限
        $pers = Permission::get();
//        获取当前角色已经被授予的权限
       $own_pers=  \DB::table('permission_role')->where('role_id',$id)->lists('permission_id');
       //dd($own_pers);
//        3 将相关的数据（角色、权限）返回给授权页面
        return view('admin.role.auth',compact('role','pers','own_pers'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $role = Role::find($id);
        return view('admin.role.edit',compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $input = Input::except('_method','_token');
        $role = Role::find($id);
        $re = $role->update($input);
        if($re){
            return redirect('admin/role');
        }else{
            return back()->with('errors','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
       $re =  Role::find($id)->delete();
       if($re){
           $data =[
             'status'=>0,
               'msg'=>'删除成功'
           ];
       }else{
           $data =[
               'status'=>1,
               'msg'=>'删除失败'
           ];
       }
       return $data;
    }
}
